<?php

class html {

    var $_js = "";
    var $_css = "";
    var $_dev = "";
    var $_Q_script = array();
    var $_Q_css = array();

    function __construct($dev_script = 0) {
        $this->_dev = $dev_script ? "_dev" : "";
    }

    /**
     * Public function _js
     * @return _js|void
     * @param mixed|void $js
     * @param mixed|void $method
     * $method : "+" Add new script in $js
     *           "-" remove script in $js
     *           "_" remove all old script and add new script to $js
     */
    function _js($js = NULL, $method = "_", $delimiter = ",") {
        if (!$js)
            return $this->_js;
        else {
            if (!is_array($js)) {
                $js = explode($delimiter, $js);
            }
            for ($i = 0; $i < count($js); $i++) {
                $js[$i] .= $this->_dev;
            }
            switch ($method) {
                case "+":
                    $this->_Q_script = array_merge($this->_Q_script, $js);
                    break;
                case "-":
                    foreach ($js as $j) {
                        if (count(array_keys($this->_Q_script, $j)) > 0) {
                            unset($this->_Q_script[array_keys($this->_Q_script, $j)]);
                        }
                    }
                    break;
                case "_":
                    $this->_Q_script = $js;
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * Public function _js
     * @return _js|void
     * @param mixed|void $css
     * @param mixed|void $method
     * $method : "+" Add new script in $css
     *           "-" remove script in $css
     *           "_" remove all old script and add new script to $css
     */
    function _css($css = NULL, $method = "_", $delimiter = ",") {
        if (!$css)
            return $this->_js;
        else {
            if (!is_array($css)) {
                $css = explode($delimiter, $css);
            }
            for ($i = 0; $i < count($css); $i++) {
                $css[$i] .= $this->_dev;
            }
            switch ($method) {
                case "+":
                    $this->_Q_css = array_merge($this->_Q_css, $css);
                    break;
                case "-":
                    foreach ($css as $j) {
                        if (count(array_keys($this->_Q_css, $j)) > 0) {
                            unset($this->_Q_css[array_keys($this->_Q_css, $j)]);
                        }
                    }
                    break;
                case "_":
                    $this->_Q_css = $css;
                    break;
                default:
                    break;
            }
        }
    }

    function explode_Q_HeaderScript() {
        global $_Q_script, $_Q_css, $_Q_HeaderScript, $base_path, $template_path;
        if ($_Q_HeaderScript) {
            $script = $css = "";
            if ($_Q_script) {
                $this->_js($_Q_script, "+");
                $script = $this->explodeJsHtml(($base_path ? "/$base_path" : "") . $template_path . "js/");
            }
            if ($_Q_css) {
                $this->_css($_Q_css, "+");
                $css = $this->explodeCssHtml(($base_path ? "/$base_path" : "") . $template_path . "css/");
            }
            $header = $script . $css;
            return $header;
        }
        return "";
    }

    function explodeJsHtml($path, $seperate = ",") {
        if (!$this->_Q_script) {
            return NULL;
        }
        if (!is_array($this->_Q_script)) {
            $this->_Q_script = explode($seperate, $this->_Q_script);
        }
        $script = "";
        // print_r($css);
        $ie = 0;
        foreach ($this->_Q_script as $j) {
            if (stristr($j, "[ie]")) {
                $j = substr($j, 4);
                $script.="<!--[if IE]>";
                $ie = 1;
            }
            if (stristr($j, "<script"))
                $script.="$j";
            else if (stristr($j, "http://")) {
                $script.="<script type='text/javascript' language='javascript' src='" . $j . ".js'></script>\n";
            }
            else
                $script.="<script type='text/javascript' language='javascript' src='" . $path . $j . ".js'></script>\n";
            if ($ie) {
                $script.="<![endif]-->";
                $ie = 0;
            }
        }
        return $script;
    }

    function explodeCssHtml($path, $seperate = ",") {
        if (!$this->_Q_css) {
            return NULL;
        }
        if (!is_array($this->_Q_css)) {
            $this->_Q_css = explode($seperate, $this->_Q_css);
        }
        $link = "";
        $ie = 0;
        // print_r($css);
        $str_ie = "";
        foreach ($this->_Q_css as $c) {
//            if(stristr($c, "[ie")){
//                $x=6;
//                while($x<12){
//                    if(stristr($c, "[ie$x")){
//                        $str_ie=
//                    }
//                }
//            }
            if (stristr($c, "[ie]")) {
                $c = str_replace('[ie]', "", $c);
                $link.="<!--[if IE]>";
                $ie = 1;
            }
            if (stristr($c, "<link"))
                $link.="$c";
            else if (stristr($c, "http://")) {
                if (stristr($c, ".css")) {
                    $link.="<link rel='stylesheet' href='" . $c . "' type='text/css' media='screen' />\n";
                }
                else
                    $link.="<link rel='stylesheet' href='" . $c . ".css' type='text/css' media='screen' />\n";
            }
            else
                $link.="<link rel='stylesheet' href='" . $path . $c . ".css' type='text/css' media='screen' />\n";
            if ($ie) {
                $link.="<![endif]-->";
                $ie = 0;
            }
        }
        return $link;
    }

}

?>